package net.trustie.model;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractBy.Source;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

@ExtractBy("//div[@class='main']/div[@class='persion_section']")
public class csdn_ask_Model implements AfterExtractor, ValidateExtractor {
	private String issueId = "0";

	private String issueUrl = "";

	private String pageMD5;

	private String history = "0";

	private String tag;

	private String extractTime;

	@ExtractBy("//*div[@class='questions_detail_con']/dl/dt/text()")
	private String issueTitle = "";

	@ExtractBy("//*div[@class='persion_section']/div[@class='common_con clearfix']/div[@class='questions_detail_con']/dl/dd/p/allText()")
	private String issueContent = "";

	@ExtractBy("//*div[@class='share_bar_con share_bar_con_01']/a[1]/text()")
	private String voteNum;

	@ExtractBy(value = "//div[@class='questions_detail_con']/dl/div[@class='tags']/a/allText()")
	private List<String> tags;

	@ExtractBy("//*div[@class='persion_section']/div[@class='common_con clearfix']/div[@class='q_operate']/p/span/allText()")
	private String postTime = "";

	/*@ExtractBy(value = "//*div[@class='persion_article']/div[@class='mod_user_info']/div[@class='user_info']/div[@class='info_box clearfix']/dl[@class='person_info']/dt/a/text()", source = Source.RawHtml)*/
	@ExtractBy(value="//*div[@class='common_con clearfix']/div[@class='q_operate']/p/span/a/text()", source = Source.RawHtml)
	private String author = "";

	@ExtractBy(value = "//*div[@class='common_con clearfix']/div[@class='q_operate']/p/span/a/@href", source = Source.RawHtml)
	private String authorUrl;

	@ExtractBy("//*[@class='answer_sort_con  q_operate']/p/text()")
	private String answerNum;
	@ExtractBy("//*[@class='share_bar_con share_bar_con_01']/a[2]/text()")
	private String viewNum;
	@ExtractBy("//*[@class='share_bar_con share_bar_con_01']/a[3]/text()")
	private String starNum;
	
	private String type="topic";
	private String source = "csdn ask";
	private String url_md5;
	
	@ExtractBy(value = "//*div[@class='other_ask']/div/dl/dd/a/@href",source=Source.RawHtml)
	private List<String> relatedPosts;
	private String relatedPost;
	@ExtractBy(value="//*div[@class='relevant_data']/div/dl/dd/a/@href",source=Source.RawHtml)
	private List<String> relatedDocs;
	private String relatedDoc;
	
	public void afterProcess(Page page) {
		this.relatedDoc = StringHandler.combineTags(relatedDocs);
		this.relatedPost = StringHandler.combineTags(relatedPosts);
		// 处理issueUrl
		this.issueUrl = page.getPageUrl();
		this.url_md5 = DigestUtils.md5Hex(this.issueUrl);
		// 处理issueId
		this.issueId = StringHandler
				.matchRightString(page.getPageUrl(), "\\d+");

		// 处理tag
		this.tag = StringHandler.combineTags(tags);

		// 处理extractTime
		this.extractTime = DateHandler.getExtractTime();

		// 处理postTime
		this.postTime = StringHandler.matchRightString(this.postTime,"\\d+.\\d\\d.\\d+\\s\\d+:\\d+");
		this.postTime = DateHandler.formatAllTypeDate(this.postTime,page.getTime());
//处理votenum
		this.voteNum=StringHandler.subString(this.voteNum, "同问");
		//处理viewnum
				this.viewNum=StringHandler.subString(this.viewNum, "浏览");
		// 处理answerNum
		if (StringUtils.isNotBlank(this.answerNum))
			this.answerNum = StringHandler.matchRightString(this.answerNum,
					"\\d+");
		else
			this.answerNum = "0";

		if(starNum == null || starNum.isEmpty())
			starNum = "收藏0";
		this.starNum = this.starNum.trim();
		this.starNum = this.starNum.substring("收藏".length(), this.starNum.length());
	
		this.pageMD5 = DigestUtils.md5Hex(this.issueTitle + this.answerNum
				+ this.voteNum);

	}

	
	public String getStarNum() {
		return starNum;
	}


	public void setStarNum(String starNum) {
		this.starNum = starNum;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getSource() {
		return source;
	}


	public void setSource(String source) {
		this.source = source;
	}


	public String getUrl_md5() {
		return url_md5;
	}


	public void setUrl_md5(String url_md5) {
		this.url_md5 = url_md5;
	}


	public List<String> getRelatedPosts() {
		return relatedPosts;
	}


	public void setRelatedPosts(List<String> relatedPosts) {
		this.relatedPosts = relatedPosts;
	}


	public String getRelatedPost() {
		return relatedPost;
	}


	public void setRelatedPost(String relatedPost) {
		this.relatedPost = relatedPost;
	}


	public List<String> getRelatedDocs() {
		return relatedDocs;
	}


	public void setRelatedDocs(List<String> relatedDocs) {
		this.relatedDocs = relatedDocs;
	}


	public String getRelatedDoc() {
		return relatedDoc;
	}


	public void setRelatedDoc(String relatedDoc) {
		this.relatedDoc = relatedDoc;
	}


	public String getViewNum() {
		return viewNum;
	}

	public void setViewNum(String viewNum) {
		this.viewNum = viewNum;
	}

	@Override
	public void validate(Page page) {
		if (StringHandler.isAtLeastOneBlank(this.issueTitle, this.author,
				this.authorUrl)) {
			page.setResultSkip(this, true);
			return;
		}
		
		if (!StringHandler.canFormatterInteger(this.issueId, this.voteNum,
				this.answerNum)) {
			page.setResultSkip(this, true);
			return;
		}
		
		if (!DateHandler.canFormatToDate(this.extractTime, this.postTime))
			page.setResultSkip(this, true);	
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getIssueId() {
		return issueId;
	}

	public void setIssueId(String issueId) {
		this.issueId = issueId;
	}

	public String getIssueTitle() {
		return issueTitle;
	}

	public void setIssueTitle(String issueTitle) {
		this.issueTitle = issueTitle;
	}

	public String getIssueContent() {
		return issueContent;
	}

	public void setIssueContent(String issueContent) {
		this.issueContent = issueContent;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getVoteNum() {
		return voteNum;
	}

	public void setVoteNum(String voteNum) {
		this.voteNum = voteNum;
	}

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public String getIssueUrl() {
		return issueUrl;
	}

	public void setIssueUrl(String issueUrl) {
		this.issueUrl = issueUrl;
	}

	public String getAuthorUrl() {
		return authorUrl;
	}

	public void setAuthorUrl(String authorUrl) {
		this.authorUrl = authorUrl;
	}

	public String getPageMD5() {
		return pageMD5;
	}

	public void setPageMD5(String pageMD5) {
		this.pageMD5 = pageMD5;
	}

	public String getHistory() {
		return history;
	}

	public void setHistory(String history) {
		this.history = history;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public String getPostTime() {
		return postTime;
	}

	public void setPostTime(String postTime) {
		this.postTime = postTime;
	}

	public String getAnswerNum() {
		return answerNum;
	}

	public void setAnswerNum(String answerNum) {
		this.answerNum = answerNum;
	}
}
